package com.pay.demo.controller;

import com.pay.demo.enums.PayStatusEnum;
import com.pay.demo.model.base.PayModel;
import com.pay.demo.model.table.PayTransaction;
import com.pay.demo.service.PayFactoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: demo
 * @description:
 * @author: guoxh
 * @create: 2019-04-01 16:37
 **/
@RestController
public class PayController {

    @Autowired
    PayFactoryService payFactoryService;

    @GetMapping("/getTransactionRecord")
    public ResponseEntity getTransactionRecord(Integer channel) {
        PayModel payModel = payFactoryService.payChannelRoute(channel);
        List<PayTransaction> records = payModel.getRecords(channel);
        return new ResponseEntity(records, HttpStatus.OK);
    }

    @PostMapping("/pay/{payStep}")
    public ResponseEntity pay(@RequestBody PayTransaction payTransaction, @PathVariable("payStep") String payStep) {
        PayModel payModel = payFactoryService.payChannelRoute(payTransaction.getPayChannel());
        payModel.setPayTransaction(payTransaction);
        return payFactoryService.payStepProcess(payModel, payStep);
    }

    /**
     * 模拟B通道支付回调，实际上可能不只有这些入参
     *
     * @param serialsNumber
     * @return
     */
    @RequestMapping("/bCallBack")
    public String bCallback(String serialsNumber, String orderStatus) {
        PayModel payModel = payFactoryService.payChannelRoute(2);
        PayTransaction payTransaction = payModel.getRecord(serialsNumber);
        payModel.setPayTransaction(payTransaction);
        if ("SUCCESS".equals(orderStatus)) {
            payModel.finishPayOrder(PayStatusEnum.ZHIFU_CHENGGONG);
        }
        return "SUCCESS";
    }
}
